<template>
	<div style="padding: 10px 36px;">
		<el-transfer class="trans" v-model="value" 
				:data="data" :titles="['待添加人员', '已添加组人员']"/>
	</div>
</template>
<script>
import Token from "@/api/token.js"
import API from "@/api/index.js"

export default {
	props: {
		//拿到父组件的任务
		id: Number
	},
	data(){
		return {
			value: [], // 已选中的人员ID（右侧）
			data: [],  // 所有人员列表
			getuser: Token.getUser(),
			assignedUserIds: [] // 初始已分配的用户ID
		}
	},
	created() {
		this.getstudent();
		this.getAssignedUsers();
	},
	
	methods: {
		getstudent(){
			const params = {
				clazz_id: this.getuser.clazz_id,
				name: this.getuser.name
			};
			API.getGroupList(params).then(response => {
				this.data = response.data.map(item => ({
					key: item.id,
					label: item.name
				}))
			});
		},
		getAssignedUsers() {
			// API.getAssignedUsers返回已分配用户id数组
			API.getAssignedUsers({
				task_id: this.id, 
				clazz_id: this.getuser.clazz_id 
			}).then(res => {
				const ids = res.data.map(item => item.id); // 提取id数组
				this.value = ids;
				console.log(this.value)
				this.assignedUserIds = [...ids];
			});
		},
		handleSave() {
			const toAdd = this.value.filter(id => !this.assignedUserIds.includes(id));
			const toRemove = this.assignedUserIds.filter(id => !this.value.includes(id));
			if (toAdd.length > 0) {
				console.log("分配任务")
				API.assignTask({ userIds: toAdd, task_id: this.id, user_id: this.getuser.id });
			}
			if (toRemove.length > 0) {
				console.log("删除任务")
				API.deleteTask({ userIds: toRemove, task_id: this.id, user_id: this.getuser.id });
			}
			this.assignedUserIds = [...this.value]; // 更新本地记录
			this.$message.success('保存成功！');
		},
		reset() {
			this.value = [...this.assignedUserIds];
		},
	},
	watch: {
		// 监听id变化后 就执行一次查询
		id(newId) {
			this.getAssignedUsers();
		}
	},
}
</script>

<style>
.trans .el-transfer__buttons {
	text-align: center;
}

.trans .el-transfer__button {
	display: block !important;
	margin: 8px 0 !important;
}

.trans .el-transfer-panel__body, .trans .el-transfer-panel__list {
	height: 500px !important;
}

@media (max-width: 500px) {
  .trans .el-transfer {
    width: 100vw !important;
    min-width: 0 !important;
    max-width: 100vw !important;
    box-sizing: border-box;
    overflow-x: hidden;
    margin: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
  }
  .trans .el-transfer-panel {
    background: var(--el-bg-color-overlay);
    box-sizing: border-box;
    display: inline-block;
    max-height: 100%;
    overflow: hidden;
    position: relative;
    text-align: left;
    vertical-align: middle;
    width: 156px;
}
  .trans .el-transfer__panel__body, .trans .el-transfer__panel__list {
    height: 220px !important;
    min-width: 0 !important;
    max-width: 100% !important;
    box-sizing: border-box;
    overflow-y: auto;
    font-size: 12px !important;
    padding: 2px 0 !important;
  }
  .trans .el-transfer__button {
    font-size: 13px !important;
    padding: 2px 6px !important;
    margin: 4px 0 !important;
  }
  .trans .el-transfer__buttons {
    flex-direction: row !important;
    justify-content: center !important;
    gap: 4px !important;
    margin: 4px 0 !important;
	padding: 0 5px;
  }
  .trans .el-transfer-panel .el-transfer-panel__header{ 
	padding-left: 2px;
  }

}
</style>